package com.wuyan.locationdog.ui.my

import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.TeamDetail
import com.arcsoft.commonbase.bean.TeamMember
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_fence_refresh
import com.arcsoft.commonbase.bus.bus_team_refresh
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.textString
import com.arcsoft.commonbase.ext.visible
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.TeamDetailPersonListAdapter
import com.wuyan.locationdog.adapter.TeamDeviceListAdapter
import com.wuyan.locationdog.databinding.TeamDetailActivityBinding
import com.wuyan.locationdog.view.InputDialog
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class TeamDetailActivity : BaseBindingActivity<TeamDetailActivityBinding>() {
    private var dialog: InputDialog? = null
    private lateinit var teamDetail: TeamDetail
    private val mViewModel: DeviceViewModel by viewModels()

    var clickPosition = 1

    private val mAdapter by lazy { TeamDeviceListAdapter(R.layout.item_team_dog, null) }
    private val mAdapterH by lazy { TeamDetailPersonListAdapter(R.layout.item_team_detail_head, null) }

    override fun initData() {

        teamDetail = intent.getSerializableExtra("bean") as TeamDetail
        setTitleText(teamDetail.teamName)

        binding.apply {

            recyclerViewH.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                adapter = mAdapterH.apply {

                    setOnItemClickListener { adapter, view, position ->
                        if (position == 0) {
                            dialog = InputDialog(mActivity, title = "添加成员", hintText = "请输入对方手机号进行添加") {
                                val map = HashMap<String, Any>()
                                map["teamId"] = teamDetail.teamId
                                map["phone"] = it
                                mViewModel.getTeamAddPerson(map)
                            }.apply {
                                show()
                                setInputType()
                            }
                        } else {
                            data.find { it.isSelect }?.isSelect = false
                            data[position].isSelect = true
                            notifyDataSetChanged()
                            mAdapter.ifSelf = data.get(position).ifSelf
                            mAdapter.setNewInstance(data.get(position)?.shareDogs?.toMutableList())
                            clickPosition = position
                            if (mAdapter.ifSelf) {
                                binding.btnSure.visible()
                            } else {
                                binding.btnSure.gone()
                            }
                        }
                    }

                }
            }
            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无猎犬", com.arcsoft.commonbase.R.mipmap.empty_default))

                    setOnItemClickListener { adapter, view, position ->
                        if (mAdapter.ifSelf) {
                            val map = HashMap<String, Any>()
                            map["teamId"] = teamDetail.teamId
                            map["deviceId"] = data[position].deviceId.toString()
                            if (data[position].ifShare) {
                                data[position].ifShare = false

                                mViewModel.teamDeviceCancel(map)
                            } else {
                                data[position].ifShare = true

                                mViewModel.teamDeviceShare(map)
                            }
                            notifyDataSetChanged()
                        }
                    }

                }
            }
            btnEdit.setOnClickListener {
                val textString = findViewById<TextView>(com.arcsoft.commonbase.R.id.tv_center_title).textString()
                dialog =InputDialog(mActivity, title = "团队名称", hintText = "团队名称", showText = textString) {
                    val map = HashMap<String, Any>()
                    map["teamId"] = teamDetail.teamId
                    map["teamName"] = it
                    mViewModel.teamUpdate(map)
                }
                dialog?.show()

            }

            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["teamId"] = teamDetail.teamId
                mViewModel.getTeamQuit(map)
            }
        }
        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["teamId"] = teamDetail.teamId
        mViewModel.getTeamDetail(map)
    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_fence_refresh, this) {
            loadNetData()
        }
        mViewModel.mTeamDetail.observe(this) {
            setTitleText(it.content?.teamName)

            val list = it.content?.teamMembers?.toMutableList()
            list?.add(0, TeamMember())
            list?.get(clickPosition)?.isSelect = true
            mAdapterH.setNewInstance(list)

            mAdapter.ifSelf = list?.get(clickPosition)?.ifSelf == true
            mAdapter.setNewInstance(list?.get(clickPosition)?.shareDogs?.toMutableList())
            if (mAdapter.ifSelf) {
                binding.btnSure.visible()
            } else {
                binding.btnSure.gone()
            }
        }
        mViewModel.anyBean.observe(this) {
            dialog?.dismiss()
            customShowLong("成功")
            loadNetData()
            Bus.post(bus_team_refresh, "")
        }
        mViewModel.deleteSuccess.observe(this) {
            customShowLong("成功")
            finish()
            Bus.post(bus_team_refresh, "")
        }
    }
}